package util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * The DBObject provides access to properties files stored in /config folder
 * 
 * @author Arne Kuhlmann, Swen Jung
 * 
 */
public class PropertiesAccess extends MyLogger {
	/** key and value of the hashmap */
	// private String key, value;
	protected static Properties stdProps = null;
	protected static String fileName="config.prop";

	/**
	 * Loads the properties file with the name filename from folder /config
	 * 
	 * @param fileName
	 * @return HashMap
	 */
	public static Properties loadProperties(String fileName) {
		// Hashmap to store propertie file entrys
		// HashMap<String, String> props = new HashMap<String, String>();
		Properties properties = new Properties();
		try {
			// properties.load(properties.getClass().getResourceAsStream("../config/"+fileName));
			properties.load(new FileInputStream("./config/" + fileName));
			/*
			 * //iterate through entrys and put them into hashmap for
			 * (Enumeration e = properties.keys(); e.hasMoreElements();) { key =
			 * (String) e.nextElement(); value = properties.getProperty(key);
			 * props.put(key, value); } return props;
			 */
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return properties;
	}

	public static String getEngineProperty(String key) {
		if (stdProps == null)
			stdProps = loadProperties(fileName);
		return stdProps.getProperty(key, null);
	}
	
	/**
	 * Saves the properties to file fileName
	 * 
	 * @param fileName
	 * @param props
	 */
	public static void saveProperties(String fileName, Properties props) {

		try {
			FileOutputStream output = new FileOutputStream("./config/" + fileName);
			props.store(output, "");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
